#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2018/3/5 11:30
# @Author   : BYF
# file      : apartment_list_page.py
# @version  :

import time, json
from base_page import BasePage
from common import function
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class ApartmentListPage(BasePage):
    # Action
    apartment_list_loc = (By.CSS_SELECTOR, "div.list-item :nth-child(1)>a")  # 门店列表
    user_loc = (By.CSS_SELECTOR, "span#user-loca")  # 选择的门店名称
    iframe_loc = (By.CSS_SELECTOR, "iframe#iframe1")  # iframe

    def _open(self):
        base_url = self.config.get("url", "base_url")
        apartment_list_url = self.config.get("url", "apartment_list_url")
        self.open(base_url + apartment_list_url)
        function.add_cookies(self.driver)
        self.open(base_url + apartment_list_url)
        self.switch_to_iframe(self.iframe_loc)

    # 获取选择的门店名称
    def get_user_loca_name(self):
        return self.driver.find_element(*self.user_loc).text

    # 选择门店
    def choose_apartment(self, apartment_name):
        self._open()
        apartment_list = self.driver.find_elements(*self.apartment_list_loc)
        time.sleep(10)

        for apartment in apartment_list:
            if apartment.text == apartment_name:
                apartment.click()
                break